/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-24     RT-Thread    first version
 */

#include "head_rtthread.h"
#include "head_user.h"

#define SWICTH 1

static  rt_thread_t VcomTx_thread;
static  rt_thread_t VcomRx_thread;

static  rt_thread_t Uart1Tx_thread;
static  rt_thread_t Uart1Rx_thread;

static  rt_thread_t Uart3Tx_thread;
static  rt_thread_t Uart3Rx_thread;

static  rt_thread_t Uart4Tx_thread;
static  rt_thread_t Uart4Rx_thread;

static  rt_thread_t Uart6Rx_thread;

static  rt_thread_t Debug_thread;
static  rt_thread_t Parame_thread;

static  rt_thread_t TaskS_thread;
        rt_thread_t CtrlS_thread;

static  rt_thread_t RC_thread;

rt_thread_t IMU_thread;
rt_thread_t AHRS_thread;
rt_thread_t Flymain_thread;

//static  rt_thread_t IO_thread;

#define VcomTx_thread_pri      15
#define VcomRx_thread_pri      14

#define Uart1Tx_thread_pri      16
#define Uart1Rx_thread_pri      16

#define Uart3Tx_thread_pri      24
#define Uart4Tx_thread_pri      24

#define Uart3Rx_thread_pri      20
#define Uart4Rx_thread_pri      24

#define Uart6Rx_thread_pri      18

#define Debug_thread_pri        11
#define Parame_thread_pri       30

#define IMU_thread_pri          17
#define AHRS_thread_pri         17

#define Flymain_thread_pri      19
#define RC_thread_pri           18

#define TaskS_thread_pri        20
#define CtrlS_thread_pri        20

int main(void)
{
#if 1 && SWICTH
    /*-----------------------------------------------------------------------------------------------------------------------*/
    /* data reading Task of icm20602*/
    IMU_thread = rt_thread_create("sensor_imuTask", sensor_imuTask, RT_NULL, 1024, IMU_thread_pri, 5);
    if(IMU_thread != RT_NULL){
        //rt_thread_startup(IMU_thread);
    }
    else{
        rt_kprintf("IMU_thread is failed to create!\r\n");
    }
    /* attitude solution */
    AHRS_thread = rt_thread_create("attitudeSolutionTask", AttitudeSolution_Task, RT_NULL, 1024, AHRS_thread_pri, 5);
    if(AHRS_thread != RT_NULL){
        //rt_thread_startup(AHRS_thread);
    }
    else{
        rt_kprintf("AHRS_thread is failed to create!\r\n");
    }
    /*-----------------------------------------------------------------*/
#endif

#if 1 && SWICTH

#ifndef AC_USING_AUTOTASK
    /* attitude solution */
    Flymain_thread = rt_thread_create("fly_Task", fly_Task, RT_NULL, 2048, Flymain_thread_pri, 5);
    if(Flymain_thread != RT_NULL){
        //rt_thread_startup(Flymain_thread);
    }
    else{
        rt_kprintf("fly_Task is failed to create!\r\n");
    }
#endif
    /* RC_Task */
    RC_thread = rt_thread_create("RC_Task", RC_Task, RT_NULL, 1024, RC_thread_pri, 5);
    if(RC_thread != RT_NULL){
        rt_thread_startup(RC_thread);
    }
    else{
        rt_kprintf("RC_Task is failed to create!\r\n");
    }
#endif

#if 1 && SWICTH

#ifdef AC_USING_AUTOTASK
    TaskS_thread = rt_thread_create("task_stream", task_stream, RT_NULL, 1024, TaskS_thread_pri, 5);
    if(TaskS_thread != RT_NULL){
        rt_thread_startup(TaskS_thread);
    }
    else{
        rt_kprintf("TaskS_thread is failed to create!\r\n");
    }
    CtrlS_thread = rt_thread_create("ctrl_stream", ctrl_stream, RT_NULL, 1024, CtrlS_thread_pri, 5);
    if(CtrlS_thread != RT_NULL){
        //rt_thread_startup(CtrlS_thread);
    }
    else{
        rt_kprintf("CtrlS_thread is failed to create!\r\n");
    }
#endif

#endif



#if 1 && SWICTH
    /*-------------------------------------------------------------------------------------------------*/
//    VcomTx_thread = rt_thread_create("vcom_txTask", vcom_txTask, RT_NULL, 1024, VcomTx_thread_pri, 10);
//    if(VcomTx_thread != RT_NULL){
//         rt_thread_startup(VcomTx_thread);
//    }
//    else{
//        rt_kprintf("uart3_txTask thread is failed to create!\r\n");
//    }
//    VcomRx_thread = rt_thread_create("vcom_rxTask", vcom_rxTask, RT_NULL, 1024, VcomRx_thread_pri, 10);
//    if(VcomRx_thread != RT_NULL){
//         rt_thread_startup(VcomRx_thread);
//    }
//    else{
//        rt_kprintf("vcom_rxTask thread is failed to create!\r\n");
//    }


    Uart1Tx_thread = rt_thread_create("uart1_txTask", uart1_txTask, RT_NULL, 1024, Uart1Tx_thread_pri, 10);
    if(Uart1Tx_thread != RT_NULL){
         rt_thread_startup(Uart1Tx_thread);
    }
    else{
        rt_kprintf("uart1_txTask thread is failed to create!\r\n");
    }
    Uart1Rx_thread = rt_thread_create("uart1_rxTask", uart1_rxTask, RT_NULL, 1024, Uart1Rx_thread_pri, 10);
    if(Uart1Rx_thread != RT_NULL){
         rt_thread_startup(Uart1Rx_thread);
    }
    else{
        rt_kprintf("uart1_rxTask thread is failed to create!\r\n");
    }


//    Uart3Tx_thread = rt_thread_create("uart3_txTask", uart3_txTask, RT_NULL, 512, Uart3Tx_thread_pri, 10);
//    if(Uart3Tx_thread != RT_NULL){
//         rt_thread_startup(Uart3Tx_thread);
//    }
//    else{
//        rt_kprintf("uart3_txTask thread is failed to create!\r\n");
//    }
    Uart3Rx_thread = rt_thread_create("uart3_rxTask", uart3_rxTask, RT_NULL, 1024, Uart3Rx_thread_pri, 10);
    if(Uart3Rx_thread != RT_NULL){
         rt_thread_startup(Uart3Rx_thread);
    }
    else{
        rt_kprintf("uart3_rxTask thread is failed to create!\r\n");
    }


//    Uart4Tx_thread = rt_thread_create("uart4_txTask", uart4_txTask, RT_NULL, 512, Uart4Tx_thread_pri, 10);
//    if(Uart4Tx_thread != RT_NULL){
//         rt_thread_startup(Uart4Tx_thread);
//    }
//    else{
//        rt_kprintf("uart4_txTask thread is failed to create!\r\n");
//    }
    Uart4Rx_thread = rt_thread_create("uart4_rxTask", uart4_rxTask, RT_NULL, 1024, Uart4Rx_thread_pri, 10);
    if(Uart4Rx_thread != RT_NULL){
         rt_thread_startup(Uart4Rx_thread);
    }
    else{
        rt_kprintf("uart4_rxTask thread is failed to create!\r\n");
    }

    Uart6Rx_thread = rt_thread_create("uart6_rxTask", uart6_rxTask, RT_NULL, 1024, Uart6Rx_thread_pri, 10);
    if(Uart6Rx_thread != RT_NULL){
         rt_thread_startup(Uart6Rx_thread);
    }
    else{
        rt_kprintf("uart6_rxTask thread is failed to create!\r\n");
    }
    /*-------------------------------------------------------------------------------------------------*/
#endif



#if 1 && SWICTH
    /*-------------------------------------------------------------------------------------------------*/
    Debug_thread = rt_thread_create("Debug_Task", debugTask, RT_NULL, 1024, Debug_thread_pri, 2);
    if(Debug_thread != RT_NULL){
         rt_thread_startup(Debug_thread);
    }
    else{
        rt_kprintf("Debug_Task thread is failed to create!\r\n");
    }
    /*-------------------------------------------------------------------------------------------------*/
#endif


#if 1 && SWICTH
    /*-------------------------------------------------------------------------------------------------*/
    /* parameters task */
    Parame_thread = rt_thread_create("Parame_Task", Parame_Task, RT_NULL, 1024, Parame_thread_pri, 10);
    if(Parame_thread != RT_NULL){
        rt_thread_startup(Parame_thread);
    }
    else{
        rt_kprintf("Parame_Task is failed to create!\r\n");
    }
    /*-------------------------------------------------------------------------------------------------*/
#endif


    static uint8_t cpu_usage_major, cpu_usage_minor;
    while (1)
    {
        cpu_usage_get(&cpu_usage_major, &cpu_usage_minor);
        rt_kprintf("CPU USAGE:%u.%u%% \r\n", cpu_usage_major, cpu_usage_minor);

        G_ioHandle.toggle(led_Red);

        rt_thread_mdelay(500);
    }

    return RT_EOK;
}




